<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0">
    <title>我的订单 - 钢域云贸</title>
    <link rel="icon" href="/uploads/logo.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="/uploads/logo.png" type="image/x-icon"/>
    <link rel="stylesheet" th:href="@{/css/tailwindcss.min.css}"/>
<!--    <script src="https://cdn.tailwindcss.com"></script>-->
    <style>
        /* 主内容区域样式 */
        main {
            display: flex;
            min-height: calc(100vh - 64px - 64px); /* 减去 header 和 footer 高度（假设各 64px） */
            width: 100%;
        }
        /* 侧边栏样式 */
        .sidebar {
            width: 256px;
            background-color: #fff;
            box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }
        /* 内容区域样式 */
        .content-area {
            flex: 1;
            padding: 2rem;
            background-color: #f7fafc;
        }
        /* 侧边栏内容区自适应 */
        .sidebar nav {
            flex-grow: 1;
        }
        /* 响应式布局 */
        @media (max-width: 640px) {
            .sidebar {
                display: none;
            }
            .content-area {
                padding: 1rem;
            }
        }
        /* 自定义按钮样式 */
        .btn-primary {
            background-color: #2563eb;
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            transition: background-color 0.2s;
        }
        .btn-primary:hover {
            background-color: #1d4ed8;
        }
        .btn-secondary {
            background-color: #e5e7eb;
            color: #374151;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            transition: background-color 0.2s;
        }
        .btn-secondary:hover {
            background-color: #d1d5db;
        }
        .btn-delete {
            background-color: #dc2626;
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            transition: background-color 0.2s;
        }
        .btn-delete:hover {
            background-color: #b91c1c;
        }
        .btn-delete-disabled {
            background-color: #fca5a5;
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 0.375rem;
            cursor: not-allowed;
        }
        /* 表单输入框样式 */
        .form-input {
            border: 1px solid #d1d5db;
            padding: 0.5rem;
            border-radius: 0.375rem;
            width: 100%;
            transition: all 0.2s;
        }
        .form-input:focus {
            outline: none;
            border-color: #2563eb;
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }
        /* 表格样式 */
        .table-custom th, .table-custom td {
            padding: 0.75rem;
            text-align: left;
        }
        .table-custom th {
            background-color: #f3f4f6;
            color: #4b5563;
            font-weight: 600;
        }
        .table-custom tr {
            border-bottom: 1px solid #e5e7eb;
        }
        .table-custom tr:hover {
            background-color: #f9fafb;
        }
        /* 分页样式 */
        .pagination a {
            padding: 0.5rem 1rem;
            margin: 0 0.25rem;
            border-radius: 0.375rem;
            color: #374151;
            background-color: #e5e7eb;
            transition: all 0.2s;
        }
        .pagination a:hover {
            background-color: #2563eb;
            color: white;
        }
        .pagination a.active {
            background-color: #2563eb;
            color: white;
        }
        .pagination a.disabled {
            background-color: #e5e7eb;
            color: #9ca3af;
            cursor: not-allowed;
        }
        /* 筛选链接样式 */
        .filter-link {
            padding: 0.5rem 1rem;
            margin-right: 0.5rem;
            border-radius: 0.375rem;
            color: #374151;
            background-color: #e5e7eb;
            transition: all 0.2s;
        }
        .filter-link:hover {
            background-color: #2563eb;
            color: white;
        }
        .filter-link.active {
            background-color: #2563eb;
            color: white;
        }
    </style>

</head>
<body class="bg-gray-100">
<!-- 公共头部 -->
<th:block th:replace="~{/header.html}"></th:block>
<!-- Main Content -->
<main>
    <!-- 侧边栏 -->
    <th:block th:replace="~{/buyer/sidebar.html}"></th:block>
    <!-- 内容区域 -->
    <div class="content-area">
        <!-- 仅在用户登录且角色为 BUYER 时显示订单内容 -->
        <div th:if="${session.user != null and session.user.role == 'BUYER'}">
            <div class="bg-white p-6 rounded-lg shadow-md">
                <!-- 消息提示 -->
                <div th:if="${success}" class="mb-4 p-4 bg-green-100 text-green-700 rounded-md" th:text="${success}"></div>
                <div th:if="${error}" class="mb-4 p-4 bg-red-100 text-red-700 rounded-md" th:text="${error}"></div>

                <!-- 订单筛选和搜索 -->
                <div class="order-filter mb-6 flex flex-wrap items-center gap-4 bg-gray-50 p-4 rounded-lg shadow-sm">
                    <form th:action="@{/buyer/list}" method="get" class="flex items-center gap-2 w-full sm:w-64">
                        <div class="relative w-full">
                            <input type="text" name="orderNo" th:value="${orderNo}" placeholder="输入订单号查询"
                                   class="pl-3 pr-9 py-2 rounded-full text-sm text-gray-800 border border-gray-300 w-full
                                          focus:outline-none focus:ring-2 focus:ring-blue-300 transition" />
                            <button type="submit" class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 hover:text-blue-600">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                          d="M21 21l-5-5m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                                </svg>
                            </button>
                        </div>
                        <input type="hidden" name="status" th:value="${status}">
                    </form>
                    <div class="flex flex-wrap gap-2">
                        <a th:href="@{/buyer/list}" th:class="${status == null}? 'filter-link active' : 'filter-link'">全部</a>
                        <a th:href="@{/buyer/list(status='PENDING')}" th:class="${status == 'PENDING'}? 'filter-link active' : 'filter-link'">待支付</a>
                        <a th:href="@{/buyer/list(status='PAID')}" th:class="${status == 'PAID'}? 'filter-link active' : 'filter-link'">待发货</a>
                        <a th:href="@{/buyer/list(status='SHIPPED')}" th:class="${status == 'SHIPPED'}? 'filter-link active' : 'filter-link'">待收货</a>
                        <a th:href="@{/buyer/list(status='COMPLETED')}" th:class="${status == 'COMPLETED'}? 'filter-link active' : 'filter-link'">已完成</a>
                        <a th:href="@{/buyer/list(status='CANCELLED')}" th:class="${status == 'CANCELLED'}? 'filter-link active' : 'filter-link'">已取消</a>
                    </div>
                </div>
                <!-- 已选择地址 -->
                <div class="selected-address mb-6" th:if="${selectedAddress != null}">
                    <h3 class="text-lg font-semibold text-gray-800 mb-2">已选择此地址</h3>
                    <div class="address-details bg-gray-50 p-4 rounded-lg shadow-sm">
                        <div class="address-header flex items-center">
                            <span class="text-gray-700" th:text="${selectedAddress.contactName + ' ' + selectedAddress.contactPhone}"></span>
                            <span class="ml-2 bg-blue-100 text-blue-700 text-xs px-2 py-1 rounded" th:if="${selectedAddress.isDefault} == 1">默认</span>
                        </div>
                        <div class="address-content text-gray-600 mt-1" th:text="${selectedAddress.deliveryAddress}"></div>
                    </div>
                </div>
                <!-- 订单列表 -->
                <table class="w-full table-custom">
                    <thead>
                    <tr>
                        <th class="text-left">订单号</th>
                        <th class="text-left">总金额</th>
                        <th class="text-left">下单时间</th>
                        <th class="text-left">状态</th>
                        <th class="text-left">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="order : ${orders}">
                        <td th:text="${order.orderNo}"></td>
                        <td th:text="'￥' + ${#numbers.formatDecimal(order.totalAmount, 1, 2)}"></td>
                        <td th:text="${#dates.format(order.createTime, 'yyyy-MM-dd HH:mm')}"></td>
                        <td>
                            <span class="order-status" th:attr="data-status=${order.status}"
                                  th:text="${#strings.equals(order.status, 'PENDING')? '待支付' :
                                             #strings.equals(order.status, 'PAID')? '已支付' :
                                             #strings.equals(order.status, 'SHIPPED')? '已发货' :
                                             #strings.equals(order.status, 'COMPLETED')? '已完成' :
                                             #strings.equals(order.status, 'CANCELLED')? '已取消' : order.status}">
                            </span>
                        </td>
                        <td>
                            <a th:href="@{'/buyer/detail/' + ${order.orderNo}}" class="text-blue-600 hover:underline flex items-center">
                                <i class="fas fa-eye mr-1"></i> 查看详情
                            </a>
                            <form th:action="@{'/buyer/delete/' + ${order.orderNo}}" method="post" style="display: inline;">
                                <button type="submit" class="text-red-600 hover:underline flex items-center">
                                    <i class="fas fa-trash-alt mr-1"></i> 删除
                                </button>
                            </form>
                        </td>
                    </tr>
                    <tr th:if="${orders.isEmpty()}">
                        <td colspan="5" class="text-center text-gray-600 py-4">暂无订单</td>
                    </tr>
                    </tbody>
                </table>
                <!-- 分页 -->
                <div class="pagination flex justify-center mt-6 space-x-2" th:if="${pageInfo != null}">
                    <a th:href="@{/buyer/list(pageNum=${pageInfo.hasPreviousPage}? ${pageInfo.prePage} : 1, status=${status}, orderNo=${orderNo})}"
                       th:classappend="${pageInfo.hasPreviousPage}? '' : 'disabled'">上一页</a>
                    <span th:each="i : ${#numbers.sequence(1, pageInfo.pages)}">
                        <a th:href="@{/buyer/list(pageNum=${i}, status=${status}, orderNo=${orderNo})}"
                           th:classappend="${i == pageInfo.pageNum}? 'active' : ''"
                           th:text="${i}"></a>
                    </span>
                    <a th:href="@{/buyer/list(pageNum=${pageInfo.hasNextPage}? ${pageInfo.nextPage} : ${pageInfo.pages}, status=${status}, orderNo=${orderNo})}"
                       th:classappend="${pageInfo.hasNextPage}? '' : 'disabled'">下一页</a>
                </div>
            </div>
        </div>
    </div>
</main>
<!-- 公共底部 -->
<th:block th:replace="~{/footer.html}"></th:block>
<script src="/js/jquery-latest.min.js"></script>
<script src="/js/popper.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>